﻿using OccurrencesCore.Model;
using OccurrencesCore.Model.Enum;
using OccurrencesCore.Service;
using OccurrencesWorkflow.BaseControl.Base;
using OccurrencesWorkflow.BaseControl.Interface;
using OccurrencesWorkflow.SpecializedControl.ScheduledReturn.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace OccurrencesWorkflow.SpecializedControl.ScheduledReturn
{
    public class ScheduledReturnWorflowControl : WorkflowControl
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="occurrenceId"></param>
        public override void Initialize(Occurrence occurrence)
        {
            //basicaly the system has to execute the first step of the workflow, in this case, send a task to the 
            //compentence approval
            using (TransactionScope scope = new TransactionScope())
            {
                List<WorkflowTask> tasksToBeCreated = BaseTask.CreateTaskObject(occurrence, WorkflowTaskTypeEnum.ScheduledReturn_CompetenceApproval);
                WorkflowTaskService service = new WorkflowTaskService();
                service.Create(tasksToBeCreated).ToList();
                scope.Complete();
            }
        }

        public override void NotifyTaskChanged(WorkflowTask task)
        {
            switch ((WorkflowTaskTypeEnum)task.WorkflowTaskTypeId)
            {
                case WorkflowTaskTypeEnum.ScheduledReturn_CompetenceApproval:
                    {
                        CompetenceApprovalActions(task);
                        break;
                    }
                case WorkflowTaskTypeEnum.ScheduledReturn_ScheduledClientCollect:
                    {

                        break;
                    }
            }
            //depending on the task status and type do something (create other tasks, change occurrence status)
            // ScheduledClientCollectTaskControl.CreateTask(task.OccurrenceId, new UserService().GetCurrentUser());
        }

        private void CompetenceApprovalActions(WorkflowTask task)
        {
            WorkflowTaskService workflowTaskService = new WorkflowTaskService();
            List<WorkflowTask> allTasks = workflowTaskService.FindByTaskTypeAndOccurrenceId(task.WorkflowTaskTypeId, task.OccurrenceId);
              
            if ((WorkflowTaskStatusEnum)task.WorkflowTaskStatusId == WorkflowTaskStatusEnum.Rejected)
            {
                //cancel all the others, return to another step...
                foreach (WorkflowTask currentTask in allTasks)
                {
                    //only change the other tasks, not the one rejected
                    if (currentTask.Id != task.Id)
                    {
                        CompetenceApprovalTaskControl taskControl = new CompetenceApprovalTaskControl(currentTask.Id);
                        taskControl.Cancel();
                    }
                    //return to SAC?
                }
            }
            else
            {
                if ((WorkflowTaskStatusEnum)task.WorkflowTaskStatusId == WorkflowTaskStatusEnum.Approved)
                {
                    bool allTasksAreApproved = true;
                    //check if all are approved, if so, move to next step
                    foreach (WorkflowTask currentTask in allTasks)
                    {
                        //only change the other tasks, not the one rejected
                        if ((WorkflowTaskStatusEnum)currentTask.WorkflowTaskStatusId != WorkflowTaskStatusEnum.Approved)
                        {
                            allTasksAreApproved = false;
                        }
                    }
                    if (allTasksAreApproved)
                    {
                        //move to next step
                    }
                }
            }
        }
    }
}
